import util from '../../utils/util.js'
import {
  getCouponsModel
} from '../../models/method.js'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    height: Number,
    limit: Number,
    status: {
      type: String,
      value: '',
      observer(newVal) {
        this.setData({
          couponList: []
        })
        this.wxRequest(1, newVal)
      }
    },
    Class: {
      type: String,
      value: "index-scrolls"
    },
    ClassNo: {
      type: String,
      value: 'index-scroll'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    couponList: [],
    page: 1,
    noMore: false,
    counts: 0,
    Text: "暂无优惠券信息"
  },
  // 在组件实例进入页面节点树时执行
  attached() {
    if (this.properties.status == '') {
      this.wxRequest(1, 'used')
    }
  },
  // 组件的方法列表
  methods: {
    wxRequest(page = 1, status = "") {
      const limit = this.properties.limit
      getCouponsModel(page, status, limit)
        .then(res => {
          if (res.data.errorCode) {
            return util.showToast('内部服务器错误')
          }
          let list = this.data.couponList
          let couponList = list.concat(res.data.list)
          let counts = res.data.counts
          couponList.forEach((item, index) => {
            couponList[index].start_time = item.start_time.slice(0, 10)
            couponList[index].end_time = item.end_time.slice(0, 10)
          })
          this.setData({
            couponList,
            page,
            counts
          })
          wx.hideLoading()
          if (this.data.counts == couponList.length) {
            this.setData({
              noMore: true
            })
          }
        })
    },
    //下拉加载
    scrollDown() {
      this.data.page++
      this.wxRequest(this.data.page, this.properties.status)
    },
    openGas(e) {
      const status = e.currentTarget.dataset.status
      if (status == "未使用") {
        wx.navigateTo({
          url: '../../pages/select/select'
        })
      }
    }
  }
})